package com.quad.innovators.salesease.config;

import com.github.xiaoymin.knife4j.core.enums.OpenAPILanguageEnums;
import com.github.xiaoymin.knife4j.spring.configuration.Knife4jHttpBasic;
import com.github.xiaoymin.knife4j.spring.configuration.Knife4jProperties;
import com.github.xiaoymin.knife4j.spring.configuration.Knife4jSetting;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;


@Configuration
public class Knife4jConfig {

    @Bean
    @Primary
    public Knife4jProperties knife4jSetting() {
        Knife4jProperties knife4jProperties = new Knife4jProperties();
        knife4jProperties.setEnable(true);
        knife4jProperties.setProduction(false);

        // Basic 认证配置
        Knife4jHttpBasic basic = new Knife4jHttpBasic();
        basic.setEnable(true);
        basic.setUsername("knife4j");
        basic.setPassword("knife4j");
        knife4jProperties.setBasic(basic);

        // 其他设置
        Knife4jSetting setting = new Knife4jSetting();
        setting.setLanguage(OpenAPILanguageEnums.ZH_CN);
        setting.setEnableVersion(true);
        setting.setEnableSwaggerModels(true);
        knife4jProperties.setSetting(setting);
        return knife4jProperties;
    }



    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            // 全局添加鉴权参数
            if(openApi.getPaths()!=null){
                openApi.getPaths().forEach((s, pathItem) -> {
                    pathItem.readOperations().forEach(operation -> {
                        operation.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
                    });
                });
            }

        };
    }

    @Bean
    public OpenAPI springShopOpenApi() {
        return new OpenAPI()
                // 接口文档标题
                .info(new Info().title("接口文档")
                        .description("api接口文档")
                        .version("1.0版本")
                );
    }
}
